const prefix = "src/productLine/DataService-WebConsoleFront";
const containersPath = prefix + "/containers";
const routePath = prefix + "/routes.js";
const templatesPrefix = "plop-template";

// 去掉开头和结尾的斜杠
function removeLeadingTrailingSlashes(path) {
    return path.replace(/^\/+|\/+$/g, "");
}
// 计算层级
function calculateHierarchy(path) {
    // 使用 '/' 分割路径
    const pathSegments = removeLeadingTrailingSlashes(path).split("/");

    // 过滤掉空字符串，确保路径不以 '/' 结尾时也能正确计算层级
    const filteredSegments = pathSegments.filter((segment) => segment !== "");

    // 计算层级
    const hierarchyLevel = filteredSegments.length;

    return hierarchyLevel;
}
// 生成相对路径
function generateRelativePath(hierarchy) {
    if (hierarchy <= 0) {
        return ""; // 如果层级数小于等于 0，返回空字符串或其他默认值
    }

    // 生成层级数量对应的相对路径
    const relativePath = Array.from({ length: hierarchy }, () => "../").join(
        "/"
    );

    return relativePath;
}

function main(plop) {
    plop.setGenerator("template", {
        description: "创建模板文件",
        // 命令行交互
        prompts: [
            {
                type: "input",
                name: "directory",
                message: "所属目录",
                default: "",
            },
            {
                type: "input",
                name: "name",
                message: "目录名称(英文名)",
                default: "demo",
            },
            {
                type: "input",
                name: "routeName",
                message: "路由名称",
                default: "测试项目",
            },
        ],
        actions: (data) => {
            const { name, routeName, directory } = data;
            const pascalCaseName = name.charAt(0).toUpperCase() + name.slice(1);
            const hierarchy = calculateHierarchy(directory); //层级
            let pathPrefix = containersPath;
            if (hierarchy > 0) {
                pathPrefix += `/${removeLeadingTrailingSlashes(directory)}/`;
            }
            const relativePath = generateRelativePath(hierarchy);

            const actions = [
                {
                    // 添加组件文件
                    type: "add",
                    // 生成的文件的地址
                    path: `${pathPrefix}/{{name}}/ManageContainer.jsx`,
                    // 模板文件地址
                    templateFile: `${templatesPrefix}/ManageContainer.jsx.hbs`,
                    skipIfExists: true,
                    data: {
                        relativePath: relativePath,
                        componentName: pascalCaseName,
                    },
                },
                {
                    // 添加样式文件
                    type: "add",
                    path: `${pathPrefix}/{{name}}/index.module.less`,
                    templateFile: `${templatesPrefix}/index.module.less.hbs`,
                    skipIfExists: true,
                    data: {
                        relativePath: relativePath,
                    },
                },
                {
                    // 添加常量文件
                    type: "add",
                    path: `${pathPrefix}/{{name}}/constants.js`,
                    templateFile: `${templatesPrefix}/constants.js.hbs`,
                    skipIfExists: true,
                    data: {
                        relativePath: relativePath,
                    },
                },
                {
                    // 添加钩子文件
                    type: "add",
                    path: `${pathPrefix}/{{name}}/hooks/useColumns.jsx`,
                    templateFile: `${templatesPrefix}/hooks/useColumns.jsx.hbs`,
                    skipIfExists: true,
                    data: {
                        relativePath: relativePath,
                    },
                },
                {
                    // 添加组件文件
                    type: "add",
                    path: `${pathPrefix}/{{name}}/components/Modal/index.jsx`,
                    templateFile: `${templatesPrefix}/components/Modal/index.jsx.hbs`,
                    skipIfExists: true,
                    data: {
                        relativePath: relativePath,
                    },
                },
                {
                    // 添加组件文件
                    type: "add",
                    path: `${pathPrefix}/{{name}}/components/SearchContainer/index.jsx`,
                    templateFile: `${templatesPrefix}/components/SearchContainer/index.jsx.hbs`,
                    skipIfExists: true,
                    data: {
                        relativePath: relativePath,
                    },
                },
                {
                    // 添加组件入口文件
                    type: "add",
                    path: `${pathPrefix}/{{name}}/components/index.js`,
                    templateFile: `${templatesPrefix}/components/index.js.hbs`,
                    skipIfExists: true,
                    data: {
                        relativePath: relativePath,
                    },
                },
                {
                    // 修改路由文件以导入新路由
                    type: "modify",
                    path: routePath,
                    pattern: /\s*export default \[/,
                    template: `\nimport ${pascalCaseName} from './containers/${
                        directory
                            ? removeLeadingTrailingSlashes(directory) + "/"
                            : ""
                    }{{name}}/ManageContainer';
\nexport default [
  {
    key: '{{name}}',
    title: '{{routeName}}',
    component: ${pascalCaseName}
  },`,
                },
            ];
            return actions;
        },
    });
}

module.exports = main;
